﻿using Cognex.VisionPro;
using log4net;
using log4net.Appender;
using log4net.Repository.Hierarchy;
using Sunny.UI;
using Sunny.UI.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using YJYpublicClass;
using YJYpublicClass.DataStruct;
using YJYpublicClass.Model;
using 焱信引导检测.CommonClass;

namespace 焱信引导检测
{
    public partial class Form1 : UIForm
    {
        public Form1()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
        }

        private Frm_Welcome welcomefrom = new Frm_Welcome();
        private Boolean InitOK = true; //初始化状态标志
        private Boolean CameraInitOK = false; //款式中相机参数初始化状态标志
        private Boolean Camera3OK = false; //款式中相机3（高度检测）是否启用标注
        private int ExitSta = 0;  //程序退出标志


        private void Form1_Load(object sender, EventArgs e)
        {
            //先把屏幕关掉
            this.Visible = false;
            this.Opacity = 0;
            Task t1 = new Task(AppINIT);
            t1.Start();
            welcomefrom.ShowDialog();
            welcomefrom.Dispose();
            //初始化之后调整窗口，启动线程
            GlobalModel._图像处理._配置完成事件 += _图像处理配置完成事件;
            GlobalModel._图像处理.Camera1 = GlobalModel.Camera;
            GlobalModel._图像处理.Camera2 = GlobalModel.Camera2;
            GlobalModel._图像处理.Camera3 = GlobalModel.Camera3;

            if (GlobalModel._高频参数._上一次打开日期.toDateTime().Date != DateTime.Today.Date)
            {
                GlobalModel._高频参数._OKnum1.setValue(2).save();
                GlobalModel._高频参数._NGnum1.setValue(0).save();
                GlobalModel._高频参数._上一次打开日期.setValue(DateTime.Now).save();
            }

            窗口调整();
            this.WindowState = FormWindowState.Maximized;
            //启动图像处理线程，处理图像
            //启动主线程
            ExitSta = 1;
            main_thread.RunWorkerAsync();
            DoOtherWork.RunWorkerAsync();
            显示_tick.Enabled = true;
            cbx_图像保存.Checked = GlobalModel._高频参数._图片保存.toBoolen();

            if (InitOK)
            {
                LogHelper.Info("软件初始化完成!");
                //触发一次自动设置
            }
            else
            {
                LogHelper.Info("程序初始化失败,请检查相关配置");
            }
            this.Visible = true;
            this.Opacity = 1;
            //  绑定显示
            cogDisplayStatusBarV21.Display = cogRecordDisplay1;
            cogDisplayStatusBarV22.Display = cogRecordDisplay2;

            if (!PLC心跳计时.IsRunning)
                PLC心跳计时.Start();
        }

        #region 初始化

        public List<string> startmes = new List<string>();

        private bool IsFormOpen(string formName)
        {
            foreach (Form form in Application.OpenForms)
            {
                if (form.Name == formName)
                {
                    return true;
                }
            }
            return false;
        }

        private void AppINIT()
        {
            try
            {
                //if(welcomefrom.)
                // InitOK = true;
                //Thread.Sleep(100);

                while (!welcomefrom.Visible)
                {
                    // Thread.Sleep(1);
                }
                //{ System.Windows.Forms.Application.DoEvents(); }
                welcomefrom.LoadingProgress(10, "系统初始化", 500);
                if (!InitSystem())
                {
                    InitOK = false;
                }
                welcomefrom.LoadingProgress(20, "读取参数", 500);
                //日志初始化,并绑定显示
                if (!ReadParameters())
                {
                    InitOK = false;
                }
                LogHelper.Info("参数加载完成");
                //运动控制卡初始化
                // Thread.Sleep(100);
                //图像处理初始化
                //相机初始化
                welcomefrom.LoadingProgress(25, "相机1初始化", 500);
                HKCamera2.camerasysteminit();
                if (!CameraInit())
                {
                    InitOK = false;
                }
                welcomefrom.LoadingProgress(30, "相机2初始化", 500);
                if (!CameraInit2())
                {
                    InitOK = false;
                }
                if (Camera3OK) {
                    welcomefrom.LoadingProgress(35, "相机3初始化", 500);
                    if (!CameraInit3())
                    {
                        InitOK = false;
                    }
                }
                welcomefrom.LoadingProgress(30, "初始化PLC通讯", 500);
                if (!GlobalModel.PLC.Init())
                {
                    InitOK = false;
                    LogHelper.Info("通讯初始化失败");
                }

                //LogHelper.Info("图像处理");
                //PLC初始化
                welcomefrom.LoadingProgress(60, "加载款式文件", 500);
                if (!kuanshiInit())
                {
                    InitOK = false;
                    LogHelper.Info("款式自动加载失败，请手动加载");
                }
                welcomefrom.LoadingProgress(100, "加载完成", 200);
                welcomefrom.CloseForm = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                LogHelper.Info(ex.ToString());
                InitOK = false;
            }
        }

        /// <summary>
        /// 初始化系统设置
        /// </summary>
        /// <returns></returns>
        private bool InitSystem()
        {
            try
            {
                //已在初始化中进行检测
                //string ProcessName = Process.GetCurrentProcess().ProcessName;
                //Process[] cur = Process.GetProcessesByName(ProcessName);
                //if (cur.Length > 1)
                //{
                //    ExitSta = 0;
                //   // MessageBox.Show("检测到进程中已有相同软件打开,请勿重复打开软件！");
                //    Application.Exit();
                //}
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        private void 窗口调整()
        {
            try
            {
                //=============================================居中显示主界面================================================
                Rectangle Rect = new Rectangle();
                Rect = Screen.GetWorkingArea(this);    //显示器分辨率
                int FormHeight = this.Height;          //窗体大小
                int FormWidth = this.Width;
                this.Location = new System.Drawing.Point((Rect.Width - FormWidth) / 2, (Rect.Height - FormHeight) / 2);
                //=============================================绑定图像窗口==================================================
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        /// <summary>
        /// 读取参数,只读取全局变量参数
        /// </summary>
        /// <returns></returns>
        ///
        //读参数，读取当前
        public Boolean ReadParameters()
        {
            try
            {
                //=============================================读取系统配置参数===============================================
                GlobalModel.AppDevice.LoadConfig();
                Camera3OK = GlobalModel.AppDevice.Config._高度检测;

                //获取log路径
                var rootAppender = ((Hierarchy)LogManager.GetRepository())
                             .Root.Appenders.OfType<FileAppender>()
                             .FirstOrDefault();
                string filename = rootAppender != null ? rootAppender.File : string.Empty;
                string direct = Path.GetDirectoryName(filename);
                GlobalModel.AppDevice.Config.LogPath = direct;
                GlobalModel._图像处理._工程路径 = GlobalModel.AppDevice.Config._VPP工程文件夹;
                //加载高频参数
                GlobalModel._高频参数._loadall();
                //日志显示绑定
                LogHelper.logEvent += LogHelperEvent;
                //产量
                //====生产数据====//

                return true;
            }
            catch (System.Exception ex)
            {
                LogHelper.Info("参数读取失败！");
                return false;
            }
        }

        public Boolean CameraInit()
        {
            GlobalModel.Camera.LoadConfig();
            LogHelper.Info("相机1编号" + GlobalModel.Camera.config.CameraSNname);
            GlobalModel.Camera.OpenCamerabyname(GlobalModel.Camera.config.CameraSNname);
            if (GlobalModel.Camera.CamreOpenFlag == 1)
            {
                GlobalModel.Camera._取图事件 += _相机1取图标志触发事件;
                GlobalModel.Camera.StartGrab();
                LogHelper.Info("相机打开成功");
                return true;
            }
            else
            {
                LogHelper.Info("相机打开失败");
                return false;
            }
        }

        public Boolean CameraInit2()
        {
            GlobalModel.Camera2.LoadConfig();
            LogHelper.Info("相机2编号" + GlobalModel.Camera2.config.CameraSNname);
            GlobalModel.Camera2.OpenCamerabyname(GlobalModel.Camera2.config.CameraSNname);
            if (GlobalModel.Camera2.CamreOpenFlag == 1)
            {
                GlobalModel.Camera2._取图事件 += _相机2取图标志触发事件;
                GlobalModel.Camera2.StartGrab();
                LogHelper.Info("相机打开成功");
                return true;
            }
            else
            {
                LogHelper.Info("相机打开失败");
                return false;
            }
        }

        public Boolean CameraInit3()
        {
            GlobalModel.Camera3.LoadConfig();
            LogHelper.Info("相机3编号" + GlobalModel.Camera3.config.CameraSNname);
            GlobalModel.Camera3.OpenCamerabyname(GlobalModel.Camera3.config.CameraSNname);
            if (GlobalModel.Camera3.CamreOpenFlag == 1)
            {
                GlobalModel.Camera3._取图事件 += _相机3取图标志触发事件;
                GlobalModel.Camera3.StartGrab();
                LogHelper.Info("相机打开成功");
                return true;
            }
            else
            {
                LogHelper.Info("相机打开失败");
                return false;
            }
        }

        //private Boolean IPInit()
        //{
        //    try
        //    {
        //        if (外设.VPPmeth.intAllvpp() == "")
        //        {
        //            // 外设.VPPmeth.cogDisplaywindow = this.cogRecordDisplay1;
        //            return true;
        //        }
        //        else
        //        {
        //            RunLogMessage("VPP加载失败");
        //            return false;
        //        }

        //    }
        //    catch (Exception ex)
        //    {
        //        RunLogMessage("加载程序异常" + ex.ToString());
        //        return false;
        //    }
        //}

        private Boolean kuanshiInit()
        {
            try
            {
                //
                string[] profiles = Directory.GetFiles(GlobalModel.AppDevice.Config._VPP工程文件夹, "*.yjye");
                filenames.Clear();
                foreach (string file in profiles)
                {
                    filenames.Add(Path.GetFileNameWithoutExtension(file));
                }
                if (filenames.Count == 0)
                {
                    LogHelper.Info("未有检测款式，请新建款式");
                    return false;
                }
                cmb_款式选择.Items.Clear();
                this.Invoke((EventHandler)delegate
                {
                    foreach (string file in filenames)
                    {
                        cmb_款式选择.Items.Add(file);
                    }
                });
                LogHelper.Info("前次款式名" + GlobalModel.AppDevice.Config._前次款式);
                if (!filenames.Contains(GlobalModel.AppDevice.Config._前次款式))
                {
                    LogHelper.Info("前次编号，未找到对应文件加载默认文件");
                    GlobalModel.AppDevice.Config._前次款式 = filenames[0];
                }
                //判断是否有这个文件
                string path = GlobalModel.AppDevice.Config._VPP工程文件夹 + "\\" + GlobalModel.AppDevice.Config._前次款式.Trim() + ".yjye";
                if (!File.Exists(path))
                {
                    LogHelper.Info(GlobalModel.AppDevice.Config._前次款式 + "该编号模型未找到，请确认是否创建");
                    return false;
                }
                ResultFunc result = GlobalModel._图像处理.Loadmode(path, out CustomModel tempmodel);
                if (result.State == Funstate.OK)
                {
                    // 外设.jiayumodel.modelone.HKgrabImage.Init(外设.HKCamera);
                    GlobalModel._图像处理.modelone = tempmodel;
                    this.Invoke((EventHandler)delegate
                    {
                        cmb_款式选择.Text = GlobalModel.AppDevice.Config._前次款式;
                        txt_当前款式名.Text = GlobalModel.AppDevice.Config._前次款式;
                    });
                    LogHelper.Info(GlobalModel.AppDevice.Config._前次款式 + "款式切换成功,");
                    GlobalModel.Camera.SetExposuretime(tempmodel._相机1曝光时间);
                    GlobalModel.Camera2.SetExposuretime(tempmodel._相机2曝光时间);
                    if (Camera3OK) {
                        GlobalModel.Camera3.SetExposuretime(tempmodel._相机3曝光时间);
                    }
                    return true;
                }
                else
                {
                    LogHelper.Info("款式切换失败," + "未能切换到" + GlobalModel.AppDevice.Config._前次款式 + "异常" + result.mes);
                    return false;
                }
            }
            catch (Exception ex)
            {
                LogHelper.Info(ex.ToString());
                return false;
            }
        }

        #endregion 初始化

        private void 相机1ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            GlobalModel.Camera.SetConfig();
        }

        private void 相机2ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            GlobalModel.Camera2.SetConfig();
        }

        private void 相机3ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            GlobalModel.Camera3.SetConfig();
        }

        private void 图像处理设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            GlobalModel._图像处理.setconfig();
        }

        private void 通讯配置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            GlobalModel.PLC.setConfig();
        }

        private void 系统配置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            GlobalModel.AppDevice.setConfig();
        }

        private void btn_手动自动_Click(object sender, EventArgs e)
        {
            //加一些自动的判断
            PublicValue._自动使能 = !PublicValue._自动使能;
        }

        private void btn_款式切换_Click(object sender, EventArgs e)
        {
            //根据当前名字加载款式，并初始化
            string path = GlobalModel.AppDevice.Config._VPP工程文件夹 + "\\" + cmb_款式选择.Text.ToString() + ".yjye";
            if (!File.Exists(path))
            {
                LogHelper.Info("该编号模型未找到，请确认是否创建");
                return;
            }
            ResultFunc result = GlobalModel._图像处理.Loadmode(path, out CustomModel tempmodel);
            if (result.State == Funstate.OK)
            {
                //原模型编号，序号
                string lastbianhao = GlobalModel._图像处理.modelone._款式名称;
                GlobalModel._图像处理.modelone = tempmodel;

                LogHelper.Info("款式切换成功," + "由原款式:" + lastbianhao + "切换到：新款式：" + tempmodel._款式名称);
            }
            else
            {
                LogHelper.Info("款式切换失败," + "未能切换到" + cmb_款式选择.ToString() + "异常" + result.mes);
            }
            GlobalModel.AppDevice.Config._前次款式 = GlobalModel._图像处理.modelone._款式名称;
            txt_当前款式名.Text = GlobalModel.AppDevice.Config._前次款式;
            GlobalModel.AppDevice.Saveconfig();
            GlobalModel.Camera.SetExposuretime(tempmodel._相机1曝光时间);
            GlobalModel.Camera2.SetExposuretime(tempmodel._相机2曝光时间);
            if (Camera3OK){
                GlobalModel.Camera3.SetExposuretime(tempmodel._相机3曝光时间);
            }
        }

        //通用图像
        private ICogImage _加载图像 = null;

        private void btn_清除_Click(object sender, EventArgs e)
        {
            GlobalModel._高频参数._OKnum1.setValue(0).save();
            GlobalModel._高频参数._NGnum1.setValue(0).save();
        }

        private void btn_更多日志_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start(GlobalModel.AppDevice.Config.LogPath);
        }

        private void btn_查看图片_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start(GlobalModel.AppDevice.Config.SaveImagePath);
        }

        private void btn_加载图片_Click(object sender, EventArgs e)
        {
            OpenFileDialog filedialog = new OpenFileDialog();
            filedialog.Filter = "图片|*.bmp;*.jpg;*.tiff;*.png";
            if (filedialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                VisionpComFuc.Loadimage(filedialog.FileName, out _加载图像);
                //显示
                VisionpComFuc.Dispimage(cogRecordDisplay1, _加载图像);
            }
        }

        private void btn_工位1测试_Click(object sender, EventArgs e)
        {
            //分两类，如果是。
            Task.Run(_相机1手动执行);
        }

        private void btn_工位2测试_Click(object sender, EventArgs e)
        {
            Task.Run(_相机2手动执行);
        }

        private void btn_工位3测试_Click(object sender, EventArgs e)
        {
            if (Camera3OK)
            {
                Task.Run(_相机3手动执行);
            }
        }

        private List<string> filenames = new List<string>();

        private void _款式搜索()
        {
            string[] profiles = Directory.GetFiles(GlobalModel.AppDevice.Config._VPP工程文件夹, "*.yjye");
            filenames.Clear();
            foreach (string file in profiles)
            {
                filenames.Add(Path.GetFileNameWithoutExtension(file));
            }
            //if (filenames.Count==0)
            //{
            //    LogHelper.Info("未检测到款式，请新建款式");
            //    return;
            //}
            cmb_款式选择.Items.Clear();
            this.Invoke((EventHandler)delegate
            {
                foreach (string file in filenames)
                {
                    cmb_款式选择.Items.Add(file);
                    //先查找款式集合中是否有这个名字，如果没有则添加
                    if (GlobalModel.AppDevice.Config._款式集合.FindIndex(x => x._款式名 == file) == -1)
                    {
                        //
                        int index = GlobalModel.AppDevice.Config._款式集合.Count;
                        while (GlobalModel.AppDevice.Config._款式集合.FindIndex(x => x._款式序号 == index) != -1)
                        {
                            index++;
                        }
                        GlobalModel.AppDevice.Config._款式集合.Add(new _款式索引(file, index));
                    }
                }
            });

            cmb_款式选择.Text = GlobalModel._图像处理.modelone._款式名称;
        }

        private Boolean _相机1收到图片标志 = false;
        private Boolean _相机2收到图片标志 = false;
        private Boolean _相机3收到图片标志 = false;
        private Boolean _相机1处理完成标志 = true;
        private Boolean _相机2处理完成标志 = true;
        private Boolean _相机3处理完成标志 = true;

        private int _相机1处理款式 = -1;

        private int _相机2处理款式 = -1;

        private int _相机3处理款式 = -1;

        private void main_thread_DoWork(object sender, DoWorkEventArgs e)
        {
            while (ExitSta == 1)
            {
                //同时执行
                if (PublicValue._自动使能)
                {
                    if (_相机1收到图片标志)
                    {
                        LogHelper.Info("相机1收到图片");
                        _相机1收到图片标志 = false;

                        if (_相机1处理完成标志)
                        {
                            RunLogMessageOut("启动线程");
                            _相机1处理完成标志 = false;
                            Task t1 = new Task(_相机1执行);
                            t1.Start();
                        }
                        else
                        {
                            RunLogMessageOut("相机1正常处理中，触发忽略");
                        }
                    }
                    if (_相机2收到图片标志)
                    {
                        LogHelper.Info("相机2收到图片");
                        _相机2收到图片标志 = false;

                        if (_相机2处理完成标志)
                        {
                            RunLogMessageOut("启动线程");
                            _相机2处理完成标志 = false;
                            Task t1 = new Task(_相机2执行);
                            t1.Start();
                        }
                        else
                        {
                            RunLogMessageOut("相机2正常处理中，触发忽略");
                        }
                    }
                    if (_相机3收到图片标志)
                    {
                        LogHelper.Info("相机3收到图片");
                        _相机3收到图片标志 = false;

                        if (_相机3处理完成标志)
                        {
                            RunLogMessageOut("启动线程");
                            _相机3处理完成标志 = false;
                            Task t1 = new Task(_相机3执行);
                            t1.Start();
                        }
                        else
                        {
                            RunLogMessageOut("相机3正常处理中，触发忽略");
                        }
                    }
                }

                Thread.Sleep(5);
            }
        }

        private void _相机1取图标志触发事件()
        {
            _相机1收到图片标志 = true;
        }

        private void _相机2取图标志触发事件()
        {
            _相机2收到图片标志 = true;
        }

        private void _相机3取图标志触发事件()
        {
            _相机3收到图片标志 = true;
        }

        private void DoOtherWork_DoWork(object sender, DoWorkEventArgs e)
        {
            while (ExitSta == 1)
            {
                try
                {
                    //自动删除超出间隔时间的文件
                    PublicFuntions.DeleateFile(GlobalModel.AppDevice.Config.SaveImagePath + "\\OKImage", GlobalModel.AppDevice.Config.DeleteImageTime);  //OK路径下的图片
                    PublicFuntions.DeleateFile(GlobalModel.AppDevice.Config.SaveImagePath + "\\NGImage", GlobalModel.AppDevice.Config.DeleteImageTime);  //NG路径下的图片

                    PublicFuntions.DeleateFile(GlobalModel.AppDevice.Config.LogPath, GlobalModel.AppDevice.Config.DeleteLogTime);                        //日志记录
                }
                catch (Exception ex)
                {
                    PublicFuntions.SaveErrorLog(GlobalModel.AppDevice.Config.LogPath, ex.ToString());
                }
                Thread.Sleep(10000);
            }
        }

        private Stopwatch PLC心跳计时 = new Stopwatch();
        private Boolean _PLC心跳值 = false;

        private void 显示_tick_Tick(object sender, EventArgs e)
        {
            //状态显示
            if (GlobalModel.Camera.CamreOpenFlag == 1)
            {
                tbtn_相机1状态.BackColor = Color.Green;
            }
            else
            {
                tbtn_相机1状态.BackColor = Color.Red;
            }
            if (GlobalModel.Camera2.CamreOpenFlag == 1)
            {
                tbtn_相机2状态.BackColor = Color.Green;
            }
            else
            {
                tbtn_相机2状态.BackColor = Color.Red;
            }
            if (!Camera3OK) 
            {
                tbtn_相机3状态.BackColor = Color.Gray;
            }
            else if (GlobalModel.Camera3.CamreOpenFlag == 1)
            {
                tbtn_相机3状态.BackColor = Color.Green;
            }
            else
            {
                tbtn_相机3状态.BackColor = Color.Red;
            }
            if (GlobalModel.PLC.isConnect())
            {
                tbtn_PLC状态.BackColor = Color.Green;
            }
            else
            {
                tbtn_PLC状态.BackColor = Color.Red;
            }
            if (PublicValue._自动使能)
            {
                grb_手动操作.Enabled = false;
                btn_手动自动.BackColor = Color.Green;
                btn_手动自动.Text = "自动中";
            }
            else
            {
                btn_手动自动.BackColor = Color.Red;
                btn_手动自动.Text = "手动";
                grb_手动操作.Enabled = true;
            }

            //PLC通讯相关
            // int nowsecond = DateTime.Now.Second;
            if (PLC心跳计时.ElapsedMilliseconds > 500)
            {
                _PLC心跳值 = !_PLC心跳值;
                GlobalModel.PLC.WriteCoil(GlobalModel.PLC.plcconfig._相机心跳_地址M.dizhi.ToString(), _PLC心跳值);
                PLC心跳计时.Restart();
            }
            //
            //周期读取2个相机状态

            #region 相机1命令读取

            if (GlobalModel.PLC.ReadCoil(GlobalModel.PLC.plcconfig._相机1轴盖拍照_地址M.dizhi.ToString(), out bool value))
            {
                GlobalModel.PLC.plcconfig._相机1轴盖拍照_地址M._测量值.setvalue(value);
                if (GlobalModel.PLC.plcconfig._相机1轴盖拍照_地址M._测量值._上升沿)
                {
                    GlobalModel.PLC.plcconfig._相机1轴盖拍照_地址M._测量值._上升沿 = false;
                    _相机1处理款式 = 0;
                    //LogHelper.Info("收到PLC相机1款式" + "轴盖");
                }
            }
            if (GlobalModel.PLC.ReadCoil(GlobalModel.PLC.plcconfig._相机1中节拍照_地址M.dizhi.ToString(), out value))
            {
                GlobalModel.PLC.plcconfig._相机1中节拍照_地址M._测量值.setvalue(value);
                if (GlobalModel.PLC.plcconfig._相机1中节拍照_地址M._测量值._上升沿)
                {
                    GlobalModel.PLC.plcconfig._相机1中节拍照_地址M._测量值._上升沿 = false;
                    _相机1处理款式 = 1;
                    //LogHelper.Info("收到PLC相机1款式"+ "中节");
                }
            }

            if (GlobalModel.PLC.ReadCoil(GlobalModel.PLC.plcconfig._相机1端盖拍照_地址M.dizhi.ToString(), out value))
            {
                GlobalModel.PLC.plcconfig._相机1端盖拍照_地址M._测量值.setvalue(value);
                if (GlobalModel.PLC.plcconfig._相机1端盖拍照_地址M._测量值._上升沿)
                {
                    GlobalModel.PLC.plcconfig._相机1端盖拍照_地址M._测量值._上升沿 = false;
                    _相机1处理款式 = 2;
                    //LogHelper.Info("收到PLC相机1款式"+ "端盖");
                }
            }

            #endregion 相机1命令读取

            #region 相机2信号读取

            if (GlobalModel.PLC.ReadCoil(GlobalModel.PLC.plcconfig._相机2轴盖拍照_地址M.dizhi.ToString(), out value))
            {
                GlobalModel.PLC.plcconfig._相机2轴盖拍照_地址M._测量值.setvalue(value);
                if (GlobalModel.PLC.plcconfig._相机2轴盖拍照_地址M._测量值._上升沿)
                {
                    GlobalModel.PLC.plcconfig._相机2轴盖拍照_地址M._测量值._上升沿 = false;
                    _相机2处理款式 = 0;
                    //LogHelper.Info("收到PLC相机2款式"+ "轴盖");
                }
            }
            if (GlobalModel.PLC.ReadCoil(GlobalModel.PLC.plcconfig._相机2中节拍照_地址M.dizhi.ToString(), out value))
            {
                GlobalModel.PLC.plcconfig._相机2中节拍照_地址M._测量值.setvalue(value);
                if (GlobalModel.PLC.plcconfig._相机2中节拍照_地址M._测量值._上升沿)
                {
                    GlobalModel.PLC.plcconfig._相机2中节拍照_地址M._测量值._上升沿 = false;
                    _相机2处理款式 = 1;
                    //LogHelper.Info("收到PLC相机2款式"+ "中节");
                }
            }

            #endregion 相机2信号读取

            #region 相机3信号读取

            if (Camera3OK && GlobalModel.PLC.ReadCoil(GlobalModel.PLC.plcconfig._相机3中节拍照_地址M.dizhi.ToString(), out value))
            {
                GlobalModel.PLC.plcconfig._相机3中节拍照_地址M._测量值.setvalue(value);
                if (GlobalModel.PLC.plcconfig._相机3中节拍照_地址M._测量值._上升沿)
                {
                    GlobalModel.PLC.plcconfig._相机3中节拍照_地址M._测量值._上升沿 = false;
                    _相机3处理款式 = 1;
                    //LogHelper.Info("收到PLC相机3款式"+ "中节");
                }
            }

            if (Camera3OK && GlobalModel.PLC.ReadCoil(GlobalModel.PLC.plcconfig._相机3端盖拍照_地址M.dizhi.ToString(), out value))
            {
                GlobalModel.PLC.plcconfig._相机3端盖拍照_地址M._测量值.setvalue(value);
                if (GlobalModel.PLC.plcconfig._相机3端盖拍照_地址M._测量值._上升沿)
                {
                    GlobalModel.PLC.plcconfig._相机3端盖拍照_地址M._测量值._上升沿 = false;
                    _相机3处理款式 = 2;
                    //LogHelper.Info("收到PLC相机3款式"+ "端盖");
                }
            }
            #endregion 相机3信号读取



            //if( GlobalModel.PLC.ReadD(GlobalModel.PLC.plcconfig._相机1检测类别_地址DU, out int value2))
            // {
            //     if (_相机2处理款式 != value2)
            //     {
            //         _相机2处理款式 = value2;
            //         LogHelper.Info("收到PLC相机2款式"+ _相机2处理款式);
            //     }
            // }

            lab_OKNUM.Text = GlobalModel._高频参数._OKnum1.Value;
            lab_NGNUM.Text = GlobalModel._高频参数._NGnum1.Value;
            lab_SUM.Text = (GlobalModel._高频参数._OKnum1.toInt() + GlobalModel._高频参数._NGnum1.toInt()).ToString();
        }

        private void RunLogMessageOut(string str)
        {
            LogHelper.Info(str);
        }

        public void _相机1执行()
        {
            Stopwatch onesteptime = new Stopwatch();
            Stopwatch runsteptime = new Stopwatch();
            //
            string PLCsend_完成 = "";
            string PLCsend_结果 = "";
            string PLCsend_数据 = "";
            int runstep = -1;
            while (true)
            {
                try
                {
                    switch (runstep)
                    {
                        case -1:
                            //收到图片，首先赋值给图像处理
                            GlobalModel._图像处理._设置检测1输入图像(GlobalModel.Camera.OutVisionImage);
                            VisionpComFuc.Dispimage(cogRecordDisplay1, GlobalModel.Camera.OutVisionImage);
                            if (PublicValue._启用检测)
                            {
                                runstep = 0;
                            }
                            else
                            {
                                if (cbx_图像保存.Checked)
                                {
                                    //保存图像
                                    VisionpComFuc.Saveimage(GlobalModel.AppDevice.Config.SaveImagePath, "1", true, cogRecordDisplay1);
                                }
                                runstep = 10;
                            }
                            break;

                        case 0:
                            //图像处理
                            if (_相机1处理款式 == -1)
                            {
                                LogHelper.Info("未收到款式信号，本次不检测");
                                _标签显示(lab_相机1OKNG, cogRecordDisplay1, GlobalModel._图像处理.disprecord_相机1, false);
                                VisionpComFuc.Saveimage(GlobalModel.AppDevice.Config.SaveImagePath, "1", false, cogRecordDisplay1);
                                //VisionpComFuc.SaveimageDispjietu(GlobalModel.AppDevice.Config.SaveImagePath, "NG", "1_0", cogRecordDisplay1);
                                runstep = 10;
                                break;
                            }
                            LogHelper.Info("收到PLC相机1款式为:" + _相机1处理款式);
                            string 款式 = _相机1处理款式 == 0 ? "轴盖" : (_相机1处理款式 == 1 ? "中结" : "尾结");
                            if (_相机1处理款式 == 0)
                            {
                                PLCsend_完成 = GlobalModel.PLC.plcconfig._相机1轴盖完成_地址M.dizhi.ToString();
                                PLCsend_结果 = GlobalModel.PLC.plcconfig._相机1轴盖结果NG_地址M.dizhi.ToString();
                                PLCsend_数据 = GlobalModel.PLC.plcconfig._相机1轴盖结果数据_地址DF.dizhi.ToString();
                            }
                            else if (_相机1处理款式 == 1)
                            {
                                PLCsend_完成 = GlobalModel.PLC.plcconfig._相机1中节完成_地址M.dizhi.ToString();
                                PLCsend_结果 = GlobalModel.PLC.plcconfig._相机1中节结果NG_地址M.dizhi.ToString();
                                PLCsend_数据 = GlobalModel.PLC.plcconfig._相机1中节结果数据_地址DF.dizhi.ToString();
                            }
                            else
                            {
                                PLCsend_完成 = GlobalModel.PLC.plcconfig._相机1端盖完成_地址M.dizhi.ToString();
                                PLCsend_结果 = GlobalModel.PLC.plcconfig._相机1端盖结果NG_地址M.dizhi.ToString();
                                PLCsend_数据 = GlobalModel.PLC.plcconfig._相机1端盖结果数据_地址DF.dizhi.ToString();
                            }
                            //清掉款式，一次处理，一次款式命令
                            _相机1处理款式 = -1;

                            if (GlobalModel._图像处理._相机1角度获取(款式, out double R).State == Funstate.OK)
                            {
                                //正常处理，发送PLC正常数据
                                GlobalModel.PLC.WriteCoil(PLCsend_完成, true);
                                GlobalModel.PLC.WriteCoil(PLCsend_结果, true);
                                GlobalModel.PLC.WriteF(PLCsend_数据, (float)R);
                                LogHelper.Info("发送PLC处理完成，结果OK，数据：" + R.ToString());
                                _标签显示(lab_相机1OKNG, cogRecordDisplay1, GlobalModel._图像处理.disprecord_相机1, true);
                                if (cbx_图像保存.Checked)
                                {
                                    //保存图像
                                    VisionpComFuc.Saveimage(GlobalModel.AppDevice.Config.SaveImagePath, "1", true, cogRecordDisplay1);
                                }
                                GlobalModel._高频参数._OKnum1.setValue(GlobalModel._高频参数._OKnum1.toInt() + 1).save();
                            }
                            else
                            {
                                GlobalModel.PLC.WriteCoil(PLCsend_完成, true);
                                GlobalModel.PLC.WriteCoil(PLCsend_结果, false);
                                //GlobalModel.PLC.WriteF(GlobalModel.PLC.plcconfig._相机1结果数据_地址DF, (float)0);
                                LogHelper.Info("发送PLC处理完成，结果NG");
                                _标签显示(lab_相机1OKNG, cogRecordDisplay1, GlobalModel._图像处理.disprecord_相机1, false);
                                VisionpComFuc.Saveimage(GlobalModel.AppDevice.Config.SaveImagePath, "1", false, cogRecordDisplay1);
                                VisionpComFuc.SaveimageDispjietu(GlobalModel.AppDevice.Config.SaveImagePath, "NG", "1_0", cogRecordDisplay1);
                                GlobalModel._高频参数._NGnum1.setValue(GlobalModel._高频参数._NGnum1.toInt() + 1).save();
                            }
                            runstep = 10;
                            break;

                        case 1:
                            //输出
                            runstep = -2;
                            break;

                        case 10:
                            _相机1处理完成标志 = true;
                            return;

                        default:
                            break;
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.Info(ex.Message);
                    //退出循环
                    _相机1处理完成标志 = true;
                    return;
                }
            }
        }

        public void _相机1手动执行()
        {
            Stopwatch onesteptime = new Stopwatch();
            Stopwatch runsteptime = new Stopwatch();
            int runstep = -2;
            while (true)
            {
                try
                {
                    switch (runstep)
                    {
                        case -2:
                            if (cbx_测试同步取图.Checked)
                            {
                                LogHelper.Info("执行软触发");
                                GlobalModel.Camera.SoftTrige();
                                onesteptime.Restart();
                                runstep = -1;
                            }
                            else
                            {
                                GlobalModel._图像处理._设置检测1输入图像(_加载图像);
                                runstep = 0;
                            }
                            break;

                        case -1:

                            if (_相机1收到图片标志)
                            {
                                _相机1收到图片标志 = false;
                                GlobalModel._图像处理._设置检测1输入图像(GlobalModel.Camera.OutVisionImage);
                                VisionpComFuc.Dispimage(cogRecordDisplay1, GlobalModel.Camera.OutVisionImage);
                                runstep = 0;
                            }
                            else
                            {
                                if (onesteptime.ElapsedMilliseconds > 1000)
                                {
                                    LogHelper.Info("取图超时");
                                    runstep = 10;
                                }
                            }
                            break;

                        case 0:
                            //图像处理
                            string 款式 = cmb_手动产品类别.Text;
                            LogHelper.Info("当前处理款式为:" + 款式);
                            if (GlobalModel._图像处理._相机1角度获取(款式, out double R).State == Funstate.OK)
                            {
                                _标签显示(lab_相机1OKNG, cogRecordDisplay1, GlobalModel._图像处理.disprecord_相机1, true);
                                if (cbx_图像保存.Checked)
                                {
                                    //保存图像
                                    VisionpComFuc.Saveimage(GlobalModel.AppDevice.Config.SaveImagePath, "1", true, cogRecordDisplay1);
                                }
                                LogHelper.Info("测得角度为:" + R.ToString());
                                dgv_show(0, R, 款式);
                            }
                            else
                            {
                                _标签显示(lab_相机1OKNG, cogRecordDisplay1, GlobalModel._图像处理.disprecord_相机1, false);
                                VisionpComFuc.Saveimage(GlobalModel.AppDevice.Config.SaveImagePath, "1", false, cogRecordDisplay1);
                                VisionpComFuc.SaveimageDispjietu(GlobalModel.AppDevice.Config.SaveImagePath, "NG", "1_0", cogRecordDisplay1);
                            }
                            runstep = 10;
                            break;

                        case 1:
                            //输出
                            runstep = -2;
                            break;

                        case 10:
                            return;

                        default:
                            break;
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.Info(ex.Message);
                    //退出循环
                    return;
                }
            }
        }

        public void _相机2手动执行()
        {
            Stopwatch onesteptime = new Stopwatch();
            Stopwatch runsteptime = new Stopwatch();
            int runstep = -2;
            while (true)
            {
                try
                {
                    switch (runstep)
                    {
                        case -2:
                            if (cbx_测试同步取图.Checked)
                            {
                                LogHelper.Info("执行软触发");
                                GlobalModel.Camera2.SoftTrige();
                                onesteptime.Restart();
                                runstep = -1;
                            }
                            else
                            {
                                GlobalModel._图像处理._设置检测2输入图像(_加载图像);
                                runstep = 0;
                            }
                            break;

                        case -1:

                            if (_相机2收到图片标志)
                            {
                                _相机2收到图片标志 = false;
                                GlobalModel._图像处理._设置检测2输入图像(GlobalModel.Camera2.OutVisionImage);
                                VisionpComFuc.Dispimage(cogRecordDisplay2, GlobalModel.Camera2.OutVisionImage);
                                LogHelper.Info("收到图像");
                                runstep = 0;
                            }
                            else
                            {
                                if (onesteptime.ElapsedMilliseconds > 1000)
                                {
                                    LogHelper.Info("取图超时");
                                    runstep = 10;
                                }
                            }
                            break;

                        case 0:
                            //图像处理
                            string 款式 = cmb_手动产品类别.Text;
                            LogHelper.Info("当前处理款式为:" + 款式);
                            if (GlobalModel._图像处理._相机2角度获取(款式, out double R).State == Funstate.OK)
                            {
                                _标签显示(lab_相机2OKNG, cogRecordDisplay2, GlobalModel._图像处理.disprecord_相机2, true);
                                if (cbx_图像保存.Checked)
                                {
                                    //保存图像
                                    VisionpComFuc.Saveimage(GlobalModel.AppDevice.Config.SaveImagePath, "2", true, cogRecordDisplay2);
                                }
                                LogHelper.Info("测得角度为:" + R.ToString());
                                //dgv_show(1, R, 款式);
                            }
                            else
                            {
                                _标签显示(lab_相机2OKNG, cogRecordDisplay2, GlobalModel._图像处理.disprecord_相机2, false);
                                //VisionpComFuc.Saveimage(GlobalModel.AppDevice.Config.SaveImagePath, "2", false, cogRecordDisplay2);
                                //VisionpComFuc.SaveimageDispjietu(GlobalModel.AppDevice.Config.SaveImagePath, "NG", "2_0", cogRecordDisplay2);
                            }
                            runstep = 10;
                            break;

                        case 1:
                            //输出
                            runstep = -2;
                            break;

                        case 10:
                            return;

                        default:
                            break;
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.Info(ex.Message);
                    //退出循环
                    return;
                }
            }
        }

        public void _相机3手动执行()
        {
            Stopwatch onesteptime = new Stopwatch();
            Stopwatch runsteptime = new Stopwatch();
            int runstep = -2;
            while (true)
            {
                try
                {
                    switch (runstep)
                    {
                        case -2:
                            if (cbx_测试同步取图.Checked)
                            {
                                LogHelper.Info("执行软触发");
                                GlobalModel.Camera3.SoftTrige();
                                onesteptime.Restart();
                                runstep = -1;
                            }
                            else
                            {
                                GlobalModel._图像处理._设置检测3输入图像(_加载图像);
                                runstep = 0;
                            }
                            break;

                        case -1:

                            if (_相机3收到图片标志)
                            {
                                _相机3收到图片标志 = false;
                                GlobalModel._图像处理._设置检测3输入图像(GlobalModel.Camera3.OutVisionImage);
                                VisionpComFuc.Dispimage(cogRecordDisplay3, GlobalModel.Camera3.OutVisionImage);
                                runstep = 0;
                            }
                            else
                            {
                                if (onesteptime.ElapsedMilliseconds > 1000)
                                {
                                    LogHelper.Info("取图超时");
                                    runstep = 10;
                                }
                            }
                            break;

                        case 0:
                            //图像处理
                            string 款式 = cmb_手动产品类别.Text;
                            LogHelper.Info("当前处理款式为:" + 款式);
                            if (GlobalModel._图像处理._相机3高度获取(款式, out double R).State == Funstate.OK)
                            {
                                _标签显示(lab_相机3OKNG, cogRecordDisplay3, GlobalModel._图像处理.disprecord_相机3, true);

                                LogHelper.Info("测得高度为:" + R.ToString());
                            }
                            else
                            {
                                _标签显示(lab_相机3OKNG, cogRecordDisplay3, GlobalModel._图像处理.disprecord_相机3, false);
                                LogHelper.Info("检测异常");
                            }
                            runstep = 10;
                            break;

                        case 1:
                            //输出
                            runstep = -2;
                            break;

                        case 10:
                            return;

                        default:
                            break;
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.Info(ex.Message);
                    //退出循环
                    return;
                }
            }
        }

        private void _标签显示(Label lab, CogRecordDisplay diswind, ICogRecord disrecod, Boolean OKNG)
        {
            this.Invoke((Action)(() =>
            {
                if (OKNG)
                {
                    lab.Text = "OK";
                    lab.BackColor = Color.Green;
                    diswind.Record = disrecod;
                    diswind.Fit();
                }
                else
                {
                    lab.Text = "NG";
                    lab.BackColor = Color.Red;
                }
            }));
        }

        public void _相机2执行()
        {
            Stopwatch onesteptime = new Stopwatch();
            Stopwatch runsteptime = new Stopwatch();
            int runstep = -1;
            string PLCsend_完成 = "";
            string PLCsend_结果 = "";
            string PLCsend_数据 = "";
            while (true)
            {
                try
                {
                    switch (runstep)
                    {
                        case -1:
                            //收到图片，首先赋值给图像处理
                            GlobalModel._图像处理._设置检测2输入图像(GlobalModel.Camera2.OutVisionImage);
                            VisionpComFuc.Dispimage(cogRecordDisplay2, GlobalModel.Camera2.OutVisionImage);
                            //
                            if (PublicValue._启用检测)
                            {
                                runstep = 0;
                            }
                            else
                            {
                                // cogRecordDisplay2.Fit();
                                if (cbx_图像保存.Checked)
                                {
                                    //保存图像
                                    VisionpComFuc.Saveimage(GlobalModel.AppDevice.Config.SaveImagePath, "2", true, cogRecordDisplay2);
                                }
                                runstep = 10;
                            }
                            break;

                        case 0:
                            //图像处理
                            LogHelper.Info("收到PLC相机2款式为:" + _相机2处理款式);
                            if (_相机2处理款式 == -1)
                            {
                                LogHelper.Info("未收到款式信号，本次不检测,无法发送结果");

                                _标签显示(lab_相机2OKNG, cogRecordDisplay2, GlobalModel._图像处理.disprecord_相机2, false);
                                VisionpComFuc.Saveimage(GlobalModel.AppDevice.Config.SaveImagePath, "2", false, cogRecordDisplay2);
                                GlobalModel._高频参数._NGnum1.setValue(GlobalModel._高频参数._NGnum1.toInt() + 1).save();
                                runstep = 10;
                                break;
                            }
                            string 款式 = _相机2处理款式 == 0 ? "轴盖" : (_相机2处理款式 == 1 ? "中节" : "尾结");

                            if (_相机2处理款式 == 0)
                            {
                                PLCsend_完成 = GlobalModel.PLC.plcconfig._相机2轴盖完成_地址M.dizhi.ToString();
                                PLCsend_结果 = GlobalModel.PLC.plcconfig._相机2轴盖结果NG_地址M.dizhi.ToString();
                                PLCsend_数据 = GlobalModel.PLC.plcconfig._相机2轴盖结果数据_地址DF.dizhi.ToString();
                            }
                            else if (_相机2处理款式 == 1)
                            {
                                PLCsend_完成 = GlobalModel.PLC.plcconfig._相机2中节完成_地址M.dizhi.ToString();
                                PLCsend_结果 = GlobalModel.PLC.plcconfig._相机2中节结果NG_地址M.dizhi.ToString();
                                PLCsend_数据 = GlobalModel.PLC.plcconfig._相机2中节结果数据_地址DF.dizhi.ToString();
                            }
                            else
                            {
                                //PLCsend_完成 =GlobalModel.PLC.plcconfig._相机2端盖完成_地址M;
                                //PLCsend_结果 = GlobalModel.PLC.plcconfig._相机1端盖结果NG_地址M;
                                //PLCsend_数据 = GlobalModel.PLC.plcconfig._相机1端盖结果数据_地址DF;
                            }
                            //清掉款式，一次处理，一次款式命令
                            _相机2处理款式 = -1;
                            if (GlobalModel._图像处理._相机2角度获取(款式, out double R).State == Funstate.OK)
                            {
                                //正常处理，发送PLC正常数据
                                GlobalModel.PLC.WriteCoil(PLCsend_完成, true);
                                GlobalModel.PLC.WriteCoil(PLCsend_结果, true);
                                GlobalModel.PLC.WriteF(PLCsend_数据, (float)R);
                                LogHelper.Info("发送PLC处理完成，结果OK，数据：" + R.ToString());
                                _标签显示(lab_相机2OKNG, cogRecordDisplay2, GlobalModel._图像处理.disprecord_相机2, true);
                                if (cbx_图像保存.Checked)
                                {
                                    //保存图像
                                    VisionpComFuc.Saveimage(GlobalModel.AppDevice.Config.SaveImagePath, "2", true, cogRecordDisplay2);
                                }
                                GlobalModel._高频参数._OKnum1.setValue(GlobalModel._高频参数._OKnum1.toInt() + 1).save();
                            }
                            else
                            {
                                GlobalModel.PLC.WriteCoil(PLCsend_完成, true);
                                GlobalModel.PLC.WriteCoil(PLCsend_结果, false);
                                // GlobalModel.PLC.WriteF(PLCsend_数据, (float)R);
                                //GlobalModel.PLC.WriteF(GlobalModel.PLC.plcconfig._相机1结果数据_地址DF, (float)0);
                                LogHelper.Info("发送PLC处理完成，结果NG");
                                _标签显示(lab_相机2OKNG, cogRecordDisplay2, GlobalModel._图像处理.disprecord_相机2, false);
                                VisionpComFuc.Saveimage(GlobalModel.AppDevice.Config.SaveImagePath, "2", false, cogRecordDisplay2);
                                VisionpComFuc.SaveimageDispjietu(GlobalModel.AppDevice.Config.SaveImagePath, "NG", "2_0", cogRecordDisplay2);
                                GlobalModel._高频参数._NGnum1.setValue(GlobalModel._高频参数._NGnum1.toInt() + 1).save();
                            }
                            runstep = 10;
                            break;

                        case 1:
                            //输出
                            runstep = -2;
                            break;

                        case 10:
                            _相机2处理完成标志 = true;
                            return;

                        default:
                            break;
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.Info(ex.Message);
                    //退出循环
                    _相机2处理完成标志 = true;
                    return;
                }
            }
        }

        public void _相机3执行()
        {
            Stopwatch onesteptime = new Stopwatch();
            Stopwatch runsteptime = new Stopwatch();
            int runstep = -1;
            string PLCsend_完成 = "";
            string PLCsend_结果 = "";
            //string PLCsend_数据 = "";

            while (true)
            {
                try
                {
                    switch (runstep)
                    {
                        case -1:
                            //收到图片，首先赋值给图像处理
                            GlobalModel._图像处理._设置检测3输入图像(GlobalModel.Camera3.OutVisionImage);
                            VisionpComFuc.Dispimage(cogRecordDisplay3, GlobalModel.Camera3.OutVisionImage);
                            //
                            if (PublicValue._启用检测)
                            {
                                runstep = 0;
                            }
                            else
                            {
                                if (cbx_图像保存.Checked)
                                {
                                    //保存图像
                                    VisionpComFuc.Saveimage(GlobalModel.AppDevice.Config.SaveImagePath, "3", true, cogRecordDisplay3);
                                }
                                runstep = 10;
                            }
                            break;

                        case 0:
                            //图像处理
                            //LogHelper.Info("收到PLC相机3款式为:"+_相机3处理款式);
                            //string 款式 = _相机1处理款式==0 ? "端盖" : (_相机1处理款式==1 ? "中结" : "尾结");
                            if (_相机3处理款式 == -1)
                            {
                                LogHelper.Info("未收到款式信号，本次不检测");
                                _标签显示(lab_相机3OKNG, cogRecordDisplay1, GlobalModel._图像处理.disprecord_相机3, false);
                                VisionpComFuc.Saveimage(GlobalModel.AppDevice.Config.SaveImagePath, "3", false, cogRecordDisplay3);

                                GlobalModel._高频参数._NGnum1.setValue(GlobalModel._高频参数._NGnum1.toInt() + 1).save();
                                runstep = 10;
                                break;
                            }
                            LogHelper.Info("收到PLC相机3款式为:" + _相机3处理款式);
                            string 款式 = _相机3处理款式 == 0 ? "轴盖" : (_相机3处理款式 == 1 ? "中节" : "端盖");
                            if (_相机3处理款式 == 0)
                            {
                                //PLCsend_完成 =GlobalModel.PLC.plcconfig._相机3轴盖完成_地址M;
                                //PLCsend_结果 = GlobalModel.PLC.plcconfig._相机3轴盖结果NG_地址M;
                                //  PLCsend_数据 = GlobalModel.PLC.plcconfig._相机3轴盖结果数据_地址DF;
                            }
                            else if (_相机3处理款式 == 1)
                            {
                                PLCsend_完成 = GlobalModel.PLC.plcconfig._相机3中节完成_地址M.dizhi.ToString();
                                PLCsend_结果 = GlobalModel.PLC.plcconfig._相机3中节结果NG_地址M.dizhi.ToString();
                                // PLCsend_数据 = GlobalModel.PLC.plcconfig._相机3中节结果数据_地址DF;
                            }
                            else
                            {
                                PLCsend_完成 = GlobalModel.PLC.plcconfig._相机3端盖完成_地址M.dizhi.ToString();
                                PLCsend_结果 = GlobalModel.PLC.plcconfig._相机3端盖结果NG_地址M.dizhi.ToString();
                                // PLCsend_数据 = GlobalModel.PLC.plcconfig._相机3端盖结果数据_地址DF;
                            }
                            //清掉款式，一次处理，一次款式命令
                            _相机3处理款式 = -1;
                            if (GlobalModel._图像处理._相机3高度获取(款式, out double R).State == Funstate.OK)
                            {
                                //正常处理，发送PLC正常数据
                                GlobalModel.PLC.WriteCoil(PLCsend_完成, true);
                                GlobalModel.PLC.WriteCoil(PLCsend_结果, true);
                                //  GlobalModel.PLC.WriteF(GlobalModel.PLC.plcconfig._相机3结果数据_地址DF, (float)R);
                                LogHelper.Info("发送PLC处理完成，结果OK，数据：" + R.ToString());
                                _标签显示(lab_相机3OKNG, cogRecordDisplay3, GlobalModel._图像处理.disprecord_相机3, true);
                                if (cbx_图像保存.Checked)
                                {
                                    //保存图像
                                    VisionpComFuc.Saveimage(GlobalModel.AppDevice.Config.SaveImagePath, "3", true, cogRecordDisplay3);
                                }
                                GlobalModel._高频参数._OKnum1.setValue(GlobalModel._高频参数._OKnum1.toInt() + 1).save();
                            }
                            else
                            {
                                GlobalModel.PLC.WriteCoil(PLCsend_完成, true);
                                GlobalModel.PLC.WriteCoil(PLCsend_结果, false);
                                //GlobalModel.PLC.WriteF(GlobalModel.PLC.plcconfig._相机1结果数据_地址DF, (float)0);
                                LogHelper.Info("发送PLC处理完成，结果NG");
                                _标签显示(lab_相机3OKNG, cogRecordDisplay1, GlobalModel._图像处理.disprecord_相机3, false);
                                VisionpComFuc.Saveimage(GlobalModel.AppDevice.Config.SaveImagePath, "3", false, cogRecordDisplay3);
                                VisionpComFuc.SaveimageDispjietu(GlobalModel.AppDevice.Config.SaveImagePath, "NG", "3_0", cogRecordDisplay3);
                                GlobalModel._高频参数._NGnum1.setValue(GlobalModel._高频参数._NGnum1.toInt() + 1).save();
                            }
                            runstep = 10;
                            break;

                        case 1:
                            //输出
                            runstep = -2;
                            break;

                        case 10:
                            _相机3处理完成标志 = true;
                            return;

                        default:
                            break;
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.Info(ex.Message);
                    //退出循环
                    _相机3处理完成标志 = true;
                    return;
                }
            }
        }

        public void LogHelperEvent(string log, int Level)
        {
            if (rtb_Log != null)
            {
                rtb_Log.Invoke((EventHandler)delegate
                {
                    if (rtb_Log.Lines.Length > 30)
                    {
                        rtb_Log.Clear();
                    }
                    rtb_Log.AppendText(log);
                    //保持在最后一行显示
                    rtb_Log.ScrollToCaret();
                });
            }
        }

        private void _图像处理配置完成事件()
        {
            _款式搜索();
        }

        private void cbx_启动检测_CheckedChanged(object sender, EventArgs e)
        {
        }

        //表格操作
        private void dgv_show(int Cameratype, double _测量值, string leibie = "")
        {
            this.Invoke(new Action(() =>
            {
                if (dgv_数据显示.Rows.Count == 0)
                {
                    dgv_数据显示.Rows.Add();
                    dgv_数据显示.Rows[dgv_数据显示.RowCount - 1].Cells[0].Value = 1;
                }
                //其实就是最后一个，最后一个就好，根据类别去判断
                switch (Cameratype)
                {
                    case 0:
                        if (dgv_数据显示.Rows[dgv_数据显示.RowCount - 1].Cells[2].Value == null)
                        {
                            dgv_数据显示.Rows[dgv_数据显示.RowCount - 1].Cells[1].Value = DateTime.Now.ToString("hh:mm:ss.fff");
                            dgv_数据显示.Rows[dgv_数据显示.RowCount - 1].Cells[2].Value = leibie;
                            dgv_数据显示.Rows[dgv_数据显示.RowCount - 1].Cells[3].Value = _测量值;
                        }
                        else
                        {
                            dgv_数据显示.Rows.Add();
                            dgv_数据显示.Rows[dgv_数据显示.RowCount - 1].Cells[0].Value = (int)dgv_数据显示.Rows[dgv_数据显示.RowCount - 2].Cells[0].Value + 1;
                            dgv_数据显示.Rows[dgv_数据显示.RowCount - 1].Cells[1].Value = DateTime.Now.ToString("hh:mm:ss.fff");
                            dgv_数据显示.Rows[dgv_数据显示.RowCount - 1].Cells[2].Value = leibie;
                            dgv_数据显示.Rows[dgv_数据显示.RowCount - 1].Cells[3].Value = _测量值;
                        }
                        break;

                    case 1:
                        if (dgv_数据显示.Rows[dgv_数据显示.RowCount - 1].Cells[4].Value == null)
                        {
                            dgv_数据显示.Rows[dgv_数据显示.RowCount - 1].Cells[4].Value = DateTime.Now.ToString("hh:mm:ss.fff");
                            dgv_数据显示.Rows[dgv_数据显示.RowCount - 1].Cells[5].Value = leibie;
                            dgv_数据显示.Rows[dgv_数据显示.RowCount - 1].Cells[6].Value = _测量值;
                        }
                        else
                        {
                            dgv_数据显示.Rows.Add();
                            dgv_数据显示.Rows[dgv_数据显示.RowCount - 1].Cells[0].Value = (int)dgv_数据显示.Rows[dgv_数据显示.RowCount - 2].Cells[0].Value + 1;
                            dgv_数据显示.Rows[dgv_数据显示.RowCount - 1].Cells[4].Value = DateTime.Now.ToString("hh:mm:ss.fff");
                            dgv_数据显示.Rows[dgv_数据显示.RowCount - 1].Cells[5].Value = leibie;
                            dgv_数据显示.Rows[dgv_数据显示.RowCount - 1].Cells[6].Value = _测量值;
                        }
                        break;

                    case 2:
                        if (dgv_数据显示.Rows[dgv_数据显示.RowCount - 1].Cells[7].Value == null)
                        {
                            dgv_数据显示.Rows[dgv_数据显示.RowCount - 1].Cells[7].Value = DateTime.Now.ToString("hh:mm:ss.fff");
                            dgv_数据显示.Rows[dgv_数据显示.RowCount - 1].Cells[8].Value = _测量值;
                        }
                        else
                        {
                            dgv_数据显示.Rows.Add();
                            dgv_数据显示.Rows[dgv_数据显示.RowCount - 1].Cells[0].Value = (int)dgv_数据显示.Rows[dgv_数据显示.RowCount - 2].Cells[0].Value + 1;
                            dgv_数据显示.Rows[dgv_数据显示.RowCount - 1].Cells[7].Value = DateTime.Now.ToString("hh:mm:ss.fff");
                            dgv_数据显示.Rows[dgv_数据显示.RowCount - 1].Cells[8].Value = _测量值;
                        }
                        break;
                }
                //如果行数超过5，则删除上面第一行
                while (dgv_数据显示.Rows.Count > 5)
                {
                    dgv_数据显示.Rows.RemoveAt(0);
                }
            }));
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                //保存位置数据

                ExitSta = 0;
                Thread.Sleep(100);
                //退出软件需要结束掉的东西
                if (GlobalModel.Camera.CamreOpenFlag == 1)
                {
                    GlobalModel.Camera.CloseCamera();
                }
                if (GlobalModel.Camera2.CamreOpenFlag == 1)
                {
                    GlobalModel.Camera2.CloseCamera();
                }
                if (Camera3OK && GlobalModel.Camera3.CamreOpenFlag == 1)
                {
                    GlobalModel.Camera3.CloseCamera();
                }
                // GlobalModel._高频参数._上一次打开日期.setValue(DateTime.Now);
                Thread.Sleep(100);
                // PublicFuntions.KillProcess("检测");    //强制结束进程
                Environment.Exit(0);
            }
            catch (Exception)
            { }
        }

        private void cbx_启动检测_Click(object sender, EventArgs e)
        {
            PublicValue._启用检测 = cbx_启动检测.Checked;
        }
    }
}